package com.orange.controller.expert;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.dbhelp.page.Page;
import com.orange.handler.AuthExportGroupMapService;
import com.orange.handler.AuthExportGroupService;
import com.orange.handler.expert.AuthExpertiseService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.expert.AuthExpertise;
import com.orange.modules.entity.expert.AuthExportGroupMap;
import com.orange.modules.entity.expert.AuthGroup;
import com.orange.util.AppConfig;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/Expertise")
public class AuthExpertiseController extends BaseController {

	private static final Logger logger = Logger.getLogger(AuthExpertiseController.class);
	
	@Autowired
	private AuthExpertiseService authExpertiseService;
	
	@Autowired
	private AppConfig appconfig;
	
	@Autowired
	private AuthExportGroupService authExportGroupService;
	
	/**
	 * 学校页面--查询专家评价
	 */
	@RequestMapping(value = "/queryAuthExpertise", produces = MediaType.APPLICATION_JSON_VALUE+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthExpertise(HttpServletRequest request,@RequestBody Map<String, String> map){
		String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(StringUtils.isBlank(schoolId)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"未登录");
		}
		try {
			AuthGroup ag = authExportGroupService.queryAuthGroupBySchoolId(schoolId);
			String groupId = ag.getPid();
			List<AuthExpertise> ae = authExpertiseService.queryAuthExpertise(groupId);
			return toResponse(ae, ReturnCode.SUCCESS, "查询成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "查询失败!");
		}
	}
	
	/**
	 * 学校--保存专家评价
	 */
	@RequestMapping(value = "/saveAuthExpertise", produces = MediaType.APPLICATION_JSON_VALUE+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveAuthExpertise(HttpServletRequest request,@RequestBody Map<String, String> map){
		String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(StringUtils.isBlank(schoolId)){
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"未登录");
		}
		try {
			AuthGroup ag = authExportGroupService.queryAuthGroupBySchoolId(schoolId);
			String groupId = ag.getPid();
			
			String workingAttitudes = map.get("workingAttitudes");
			String professionalLevels = map.get("professionalLevels");
			String evaluatioAbilitys = map.get("evaluatioAbilitys");
			String probitySelfDisciplines = map.get("probityselfDisciplines");
			String teamSpirits = map.get("teamSpirits");
			
			String[] workingAttitude = workingAttitudes.split("#");
			String[] professionalLevel = professionalLevels.split("#");
			String[] evaluatioAbility = evaluatioAbilitys.split("#");
			String[] probitySelfDiscipline = probitySelfDisciplines.split("#");
			String[] teamSpirit = teamSpirits.split("#");
			
			List<AuthExportGroupMap> list = authExportGroupService.queryAuthExportGroupExportIdByGroupId(groupId);//专家id
			for (int i = 0; i < list.size(); i++) {
				AuthExpertise ae = new AuthExpertise();
				ae.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ae.setGroupId(groupId);
				ae.setExpertId(list.get(i).getExportId());
				if(workingAttitude[i].equals(" ")){
					ae.setWorkingAttitude(null);
				}else{
					ae.setWorkingAttitude(workingAttitude[i]);
				}
				if(professionalLevel[i].equals(" ")){
					ae.setProfessionalLevel(null);
				}else{
					ae.setProfessionalLevel(professionalLevel[i]);
				}
				if(evaluatioAbility[i].equals(" ")){
					ae.setEvaluatioAbility(null);
				}else{
					ae.setEvaluatioAbility(evaluatioAbility[i]);
				}
				if(probitySelfDiscipline[i].equals(" ")){
					ae.setProbityselfDiscipline(null);
				}else{
					ae.setProbityselfDiscipline(probitySelfDiscipline[i]);
				}
				if(teamSpirit[i].equals(" ")){
					ae.setTeamSpirit(null);
				}else{
					ae.setTeamSpirit(teamSpirit[i]);
				}
				authExpertiseService.saveAuthExpertAdvice(ae);
			}
			return toResponse(null, ReturnCode.SUCCESS, "保存成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "保存失败!");
		}
		
	}
	
	/**
	 * 管理员页面--分页查询专家评价
	 */
	@RequestMapping(value = "/queyAuthExpertiseByPage", produces = MediaType.APPLICATION_JSON_VALUE+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queyAuthExpertiseByPage(HttpServletRequest request,@RequestBody Map<String, String> map){
		String pageSize = map.get("pageSize");
		String pageNumber = map.get("pageNumber");
		if(StringUtils.isBlank(pageNumber) || StringUtils.isBlank(pageSize)){
			return toResponse(null, ReturnCode.FAIL, "参数存在空值!");
		}
		try {
			Page<AuthExpertise> page = authExpertiseService.queryAuthExpertiseByPage(map);
			return toResponse(page, ReturnCode.SUCCESS, "分页查询成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "分页查询失败!");
		}
	}
	
	/**
	 * 管理员页面--查询专家建议详细信息
	 */
	@RequestMapping(value = "/queyAuthExpertiseByGroupId", produces = MediaType.APPLICATION_JSON_VALUE+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queyAuthExpertiseByGroupId(HttpServletRequest request,@RequestBody Map<String, String> map){
		String groupId = map.get("groupId");
		
		try {
			List<AuthExpertise> ae = authExpertiseService.queryAuthExpertise(groupId);
			return toResponse(ae, ReturnCode.SUCCESS, "查询成功!");
		} catch (SQLException e) {
			e.printStackTrace();
			return toResponse(null, ReturnCode.FAIL, "查询失败!");
		}
	}
}
